<?php

namespace app\api\controller;
use think\facade\Db;
use think\facade\Request;

class Menu
{
    public function menuList()
    {
        $pageData=Request::param();
        $page = $pageData['page'];
        $perPage=$pageData['pageSize'];
        
        if(isset($pageData['time'][0])){
            $start_time=$pageData['time'][0];
        }else{
            $start_time='';
        }
        if(isset($pageData['time'][1])){
            $end_time=$pageData['time'][1] . '23:59:59';
        } else{
            $end_time='';
        }
        
        if(isset($pageData['keyword'])){
            $name=$pageData['keyword'];
        } else{
            $name='';
        }

        $query=Db::name('menus')->order('listorder');
        if ($start_time) {
            $query=$query->where('create_time', '>=', $start_time);
        }
        // 如果 end_time 存在，则添加条件
        if ($end_time) {
            $query=$query->where('create_time', '<=', $end_time);
        }
        // 如果 xiangmuming 存在，则进行模糊查询
        if ($name) {
            $query=$query->where('name', 'like', '%'.$name. '%');
        }
        $userData=$query
                ->order('create_time','desc')
                ->paginate([
                        'list_rows' => $perPage,
                        'page'      => $page,
                    ]);
        $data=$userData;
        if (!$data) {
                return json([
                'message' => '没有数据', 
                'data' => '', 
                'code' => 200
            ]);
        } else {
            return json([
                'message' => '数据查询成功',
                'code'    => 200,
                'data'    => $data,
            ]);
        }
    }

    public function menuAdd()
    {
        $menuData=Request::param();
        $insertData['name']=$menuData['name'];
        $insertData['url'] =$menuData['url'];
        $insertData['parentId']=$menuData['parentId'];
        $insertData['create_time']=date('Y-m-d H:i:s');
        $insertData['update_time']=date('Y-m-d H:i:s');
        $insertData['listorder']=$menuData['listorder'];
        $res=Db::name('menus')->insert($insertData);
        if($res){
            return json([
                'code' =>200,
                'message'=>'添加成功',
            ]);
        }else{
            return json([
                'code' =>403,
                'message'=>'添加失败',
            ]);
        }
    }

    public function menuEdit()
    {
        $menuData=Request::param();
        $id=$menuData['id'];
        $newData['name']=$menuData['name'];
        $newData['parentId']=$menuData['parentId'];
        $newData['url']=$menuData['url'];
        $newData['update_time']=date('Y-m-d H:i:s');
        $newData['listorder']=$menuData['listorder'];
        $res=Db::name('menus')->where('id',$id)->update($newData);
        if($res){
            return json([
                'code' =>200,
                'message'=>'更新成功',
            ]);
        }else{
            return json([
                'code' =>403,
                'message'=>'更新失败',
            ]);
        }
    }

    public function menuDel()
    {
        $id=Request::param('id');
        $res=Db::name('menus')->where('id',$id)->delete();
        if($res){
            return json([
                'code' =>200,
                'message'=>'删除成功',
            ]);
        }else{
            return json([
                'code' =>408,
                'message'=>'删除失败',
            ]);
        }
    }
}